package co.yixiang.hw.service.impl;

import co.yixiang.hw.domain.YxStoreOrderStatus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.hw.repository.YxStoreOrderStatusRepository;
import co.yixiang.hw.service.YxStoreOrderStatusService;
import co.yixiang.hw.service.dto.YxStoreOrderStatusDto;
import co.yixiang.hw.service.dto.YxStoreOrderStatusQueryCriteria;
import co.yixiang.hw.service.mapper.YxStoreOrderStatusMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author Walert
* @date 2020-02-13
*/
@Service
//@CacheConfig(cacheNames = "yxStoreOrderStatus")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreOrderStatusServiceImpl implements YxStoreOrderStatusService {
    @Resource
    private YxStoreOrderStatusRepository yxStoreOrderStatusRepository;

    @Resource
    private YxStoreOrderStatusMapper yxStoreOrderStatusMapper;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxStoreOrderStatusQueryCriteria criteria, Pageable pageable){
        Page<YxStoreOrderStatus> page = yxStoreOrderStatusRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxStoreOrderStatusMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<YxStoreOrderStatusDto> queryAll(YxStoreOrderStatusQueryCriteria criteria){
        return yxStoreOrderStatusMapper.toDto(yxStoreOrderStatusRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxStoreOrderStatusDto findById(Integer id) {
        YxStoreOrderStatus yxStoreOrderStatus = yxStoreOrderStatusRepository.findById(id).orElseGet(YxStoreOrderStatus::new);
        ValidationUtil.isNull(yxStoreOrderStatus.getId(),"YxStoreOrderStatus","id",id);
        return yxStoreOrderStatusMapper.toDto(yxStoreOrderStatus);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxStoreOrderStatusDto create(YxStoreOrderStatus resources) {
        return yxStoreOrderStatusMapper.toDto(yxStoreOrderStatusRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreOrderStatus resources) {
        YxStoreOrderStatus yxStoreOrderStatus = yxStoreOrderStatusRepository.findById(resources.getId()).orElseGet(YxStoreOrderStatus::new);
        ValidationUtil.isNull( yxStoreOrderStatus.getId(),"YxStoreOrderStatus","id",resources.getId());
        yxStoreOrderStatus.copy(resources);
        yxStoreOrderStatusRepository.save(yxStoreOrderStatus);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxStoreOrderStatusRepository.deleteById(id);
        }
    }

    @Override
    public void asyncCreate(YxStoreOrderStatus resources) {
        this.create(resources);
    }

    @Override
    public void asyncCreate(Integer oid, String type, String message) {
        Long addTime = System.currentTimeMillis()/ 1000L;
        //生成订单状态
        YxStoreOrderStatus orderStatus = new YxStoreOrderStatus();
        orderStatus.setOid(oid);
        orderStatus.setChangeTime(addTime.intValue());
        orderStatus.setChangeType(type);
        orderStatus.setChangeMessage(message);
        this.create(orderStatus);
        System.out.println("订单状态创建完成！！！");
    }
}